package com.czr.dao.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.dao.entity.*;
import com.czr.dao.mapper.CzrNewsMapper;
import com.czr.dao.mapper.CzrSurveysMapper;
import com.czr.dao.service.CzrNewsService;
import com.czr.dao.service.CzrSurveysService;
import com.czr.dao.service.CzrUserService;
import com.czr.dto.*;
import com.czr.util.PageUtil;
import com.czr.util.TencentCosUtil;
import com.czr.vo.CzrNewsVo;
import com.czr.vo.Result;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 新闻资讯 服务实现类
 * </p>
 *
 */
@Service
public class CzrSurveysServiceImpl extends ServiceImpl<CzrSurveysMapper, CzrSurveys> implements CzrSurveysService {

    @Autowired
    public CzrSurveysMapper czrSurveysMapper;


    @Override
    public Result<Object> add(SurveysParam surveysParam) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CzrSurveys  czrSurveys=new CzrSurveys();
        czrSurveys.setDescription(surveysParam.getDescription());
        czrSurveys.setName(surveysParam.getName());
        czrSurveys.setInsertTime(sdf.format(new Date()));
        czrSurveys.setStatus(surveysParam.getStatus());
        czrSurveys.insert();
        CzrQuestions czrQuestions=new CzrQuestions();
        for (int i=0;i<surveysParam.getCzrQuestionsList().size();i++){
            czrQuestions.setText(surveysParam.getCzrQuestionsList().get(i).getText());
            czrQuestions.setType(surveysParam.getCzrQuestionsList().get(i).getType());
            czrQuestions.setSurveyId(czrSurveys.getId());
            czrSurveysMapper.addQuestions(czrQuestions);
            CzrOptions czrOptions=new CzrOptions();
            for (int j=0;j<surveysParam.getCzrQuestionsList().get(i).getCzrOptionsList().size();j++){
                czrOptions.setText(surveysParam.getCzrQuestionsList().get(i).getCzrOptionsList().get(j).getText());
                czrOptions.setQuestionId(czrQuestions.getId());
                czrSurveysMapper.addOptions(czrOptions);
            }
        }
        return Result.success(200, "发布成功！");
    }

    @Override
    public Result<Object> getAllSurveys() throws Exception {
        List<CzrSurveys> czrSurveysList=czrSurveysMapper.selectList(new LambdaQueryWrapper<CzrSurveys>().eq(CzrSurveys::getStatus, 1));
        return Result.data(200, czrSurveysList, null);
    }

    @Override
    public Result<Object> getList(Search search) throws Exception {
        Page<CzrSurveys> page = new Page<>();
        page.setSize(search.getSize());
        page.setCurrent(search.getCurrent());
        LambdaQueryWrapper<CzrSurveys> ew = new LambdaQueryWrapper<>();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(search.getKeyword())){
            ew.like(CzrSurveys::getName,search.getKeyword());
        }
        ew.in(CzrSurveys::getStatus,Arrays.asList(0,1,2));
        ew.orderByDesc(CzrSurveys::getInsertTime);
        page = this.getBaseMapper().selectPage(page,ew);
        JSONArray array=new JSONArray();
        for (CzrSurveys czrSurveys : page.getRecords()) {
            array.add(czrSurveys);
        }
        JSONObject object= PageUtil.getPage(array,page);
        return Result.data(object);

    }

    @Override
    public Result<Object> qiYong(Integer id, Integer status) throws Exception {
        CzrSurveys czrSurveys=czrSurveysMapper.selectById(id);
        if(status==1){
            czrSurveys.setStatus(1);
            czrSurveys.updateById();
            return Result.success(200, "启用成功！");
        }else if(status==2){
            czrSurveys.setStatus(2);
            czrSurveys.updateById();
            return Result.success(200, "停止成功！");
        }
        return null;
    }

    @Override
    public Result<Object> getSurveysInfo(Integer id) throws Exception {
        SurveysParam surveysParam=new SurveysParam();
        CzrSurveys czrSurveys=czrSurveysMapper.selectById(id);
        List<CzrQuestions> czrQuestionsList=czrSurveysMapper.getQuestions(id);
        for (int i=0;i<czrQuestionsList.size();i++){
            List<CzrOptions> czrOptionsList=czrSurveysMapper.getOptions(czrQuestionsList.get(i).getId());
            czrQuestionsList.get(i).setCzrOptionsList(czrOptionsList);
        }
        surveysParam.setDescription(czrSurveys.getDescription());
        surveysParam.setStatus(czrSurveys.getStatus());
        surveysParam.setName(czrSurveys.getName());
        surveysParam.setCzrQuestionsList(czrQuestionsList);
        return Result.data(200, surveysParam, null);
    }

    @Override
    public Result<Object> submitQuestionnaire(HuidaParam huidaParam) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CzrHuida czrHuida=new CzrHuida();
        czrHuida.setSurveysId(huidaParam.getSurveysId());
        czrHuida.setName(huidaParam.getName());
        czrHuida.setSex(huidaParam.getSex());
        czrHuida.setAge(huidaParam.getAge());
        czrHuida.setPhone(huidaParam.getPhone());
        czrHuida.setWx(huidaParam.getWx());
        czrHuida.setEmail(huidaParam.getEmail());
        czrHuida.setInsertTime(sdf.format(new Date()));
        czrSurveysMapper.addHuida(czrHuida);
        if(!huidaParam.getCzrAnswersList().isEmpty()){
            for (int i=0;i<huidaParam.getCzrAnswersList().size();i++){
                CzrAnswers czrAnswers = getCzrAnswers(huidaParam, i, czrHuida);
                czrSurveysMapper.addAnswer(czrAnswers);
            }
        }

        return Result.success(200, "提交成功！");
    }

    @Override
    public Result<Object> getHuidaInfo(Integer id) throws Exception {
        JSONObject object=new JSONObject();
        CzrHuida czrHuida=czrSurveysMapper.getHuidaInfo(id);
        object.put("czrHuida", czrHuida);
        if(czrHuida!=null){
            List<CzrAnswers> czrAnswersList= czrSurveysMapper.getAnswerInfo(czrHuida.getId());
            JSONArray array=new JSONArray();
            for (int j=0;j<czrAnswersList.size();j++){
                CzrQuestions czrQuestions=czrSurveysMapper.getQuestionInfo( czrAnswersList.get(j).getQuestionId());
                JSONObject object1=new JSONObject();
                object1.put("text", czrQuestions.getText());
                object1.put("type", czrQuestions.getType());
                if(czrQuestions.getType()==1){
                    String txt="";
                    if(!StringUtils.isEmpty(czrAnswersList.get(j).getOptionId())){
                        CzrOptions czrOptions=czrSurveysMapper.getOptionInfo(Long.parseLong(czrAnswersList.get(j).getOptionId()));
                        txt=czrOptions.getText();
                    }
                    object1.put("optionId",txt);
                }else if(czrQuestions.getType()==2){
                    String txt="";
                    if(!StringUtils.isEmpty(czrAnswersList.get(j).getOptionId())){
                        String[] split = czrAnswersList.get(j).getOptionId().split(",");

                        for (int k=0;k<split.length;k++){
                            CzrOptions czrOptions=czrSurveysMapper.getOptionInfo(Long.parseLong(split[k]));
                            if(k==split.length-1){
                                txt+=czrOptions.getText();
                            }else{
                                txt+=czrOptions.getText()+",";
                            }
                        }
                    }
                    object1.put("optionId", txt);
                }else if(czrQuestions.getType()==3){
                    object1.put("textAnswer", czrAnswersList.get(j).getTextAnswer());
                }
                array.add(object1);
            }
            object.put("czrAnswersList", array);

        }
        return Result.data(200, object, null);
    }



    private static CzrAnswers getCzrAnswers(HuidaParam huidaParam, int i, CzrHuida czrHuida) {
        CzrAnswers czrAnswers=new CzrAnswers();
        czrAnswers.setQuestionId(huidaParam.getCzrAnswersList().get(i).getQuestionId());
        if(huidaParam.getCzrAnswersList().get(i).getOptionId()!=null){
            czrAnswers.setOptionId(huidaParam.getCzrAnswersList().get(i).getOptionId());
        }
        if(huidaParam.getCzrAnswersList().get(i).getTextAnswer()!=null){
            czrAnswers.setTextAnswer(huidaParam.getCzrAnswersList().get(i).getTextAnswer());
        }
        czrAnswers.setHuidaId(czrHuida.getId());
        return czrAnswers;
    }
}
